$(function () {
   obj.init();
});
var obj =new function () {
   "use strict";
   return {
       form:null,
       schoolId:null,
       init:function () {
           this.form = new xhForm($('#xh_form'));
           this.form.parse();
           this.schoolId = xhTool.getQuery('schoolId');
           $('[name="classes[]"],[name="teachers[]"]').select2();
            this.listener();
       },
       listener:function () {
           var $this = this;

           //班级选择监听事件
           $('[name="classes[]"]').on('change',function () {
               $this.funObj().changeToStuNum($(this).val());
           });

           //教师选择监听事件
           $('[name="teachers[]"]').on('change',function () {
                $this.funObj().changeToTchNum($(this).val());
           });
           $('#bt_modal_confirm' , parent.document).click(function () {
               $this.funObj().submitForm();
           });
           // 全部选中
           $('#allSelect').click(function () {
               $this.funObj().selectTeacher();
           })
       },
       funObj:function () {
           var $this = this;
           return {
               changeToStuNum:function (val) {
                   if (val) {
                       xhExtra.ajaxRequest('bpm/plan/getStuNum',{schoolId:$this.schoolId,classes:val},{},function (res) {
                           $('[name=stuTabletNum]').val(res.total);
                       })
                   } else {
                       $('[name=stuTabletNum]').val(0);
                   }
               },
               changeToTchNum:function (val) {
                   // console.log(val);
                   if (!val) {
                       $('[name=tchTabletNum]').val(0);
                   } else {
                       $('[name=tchTabletNum]').val(val.length);
                   }

                    // if(val.length > 0){
                    //     xhExtra.ajaxRequest('bpm/plan/getTchNum',{teachers:val},{},function (res) {
                    //         $('[name=tchTabletNum]').val(res.total);
                    //     })
                    // }
                },
               submitForm:function () {
                   if( $('[name=stuTabletNum]').val() == 0 && $('[name=tchTabletNum]').val() == 0 ){
                       window.top.xhTop.showError('错误','申请的平板数不能为零');
                       return false;
                   }
                   $this.form.submit('bpm/plan/saveApply',{},function (res) {
                       window.top.xhTop.showSuccess(res.msg);
                       window.top.xhTop.closeModal();
                   });
               },
               selectTeacher:function () {
                   var arr = [];
                   $.each(teachers,function (id,text) {
                       arr.push(id);
                   });
                   $('[name="teachers[]"]').val(arr).trigger('change');
               }
           }
       }
   }
};